package top.songxs.gmall.product.rpc.biz.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.songxs.gmall.common.result.Result;
import top.songxs.gmall.product.entity.SkuInfo;
import top.songxs.gmall.product.rpc.biz.*;
import top.songxs.gmall.product.vo.AttrValuesVo;
import top.songxs.gmall.product.vo.SkuDetailVo;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SkuDetailVoRpcServiceImpl implements SkuDetailVoRpcService {
    @Autowired
    private CategoryFeignRpcService categoryFeignRpcService;

    @Autowired
    private SkuInfoFeignRpcService skuInfoFeignRpcService;

    @Autowired
    private SpuSaleAttrRpcService spuSaleAttrRpcService;

    @Autowired
    private SkuSaleValuesConcatRpcService skuSaleValuesConcatRpcService;

    @Override
    public Result<SkuDetailVo> getSkuDetailVo(Long skuId) {
        SkuDetailVo skuDetailVo = new SkuDetailVo();
        skuDetailVo.setCategoryView(categoryFeignRpcService.findAllCategory(skuId));
        SkuInfo skuInfo = skuInfoFeignRpcService.getSkuInfo(skuId);
        skuDetailVo.setSkuInfo(skuInfo);
        skuDetailVo.setPrice(skuInfo.getPrice());
        skuDetailVo.setSpuSaleAttrList(spuSaleAttrRpcService.findSpuSalAttrAndValueBySkuId(skuId));
        List<AttrValuesVo> skuSaleValues = skuSaleValuesConcatRpcService.getSkuSaleValues(skuId);
        Map<String, Long> resultMap = skuSaleValues.stream().collect(Collectors.toMap(AttrValuesVo::getSavIds, AttrValuesVo::getSkuId));
        String jsonData = JSON.toJSONString(resultMap);
        skuDetailVo.setValuesSkuJson(jsonData);
        return Result.ok(skuDetailVo);
    }
}
